package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cskaoyan.wordmemorize.common.constant.ResultCodeEnum;
import com.cskaoyan.wordmemorize.common.exception.BusinessException;
import com.cskaoyan.wordmemorize.converter.AdminLogConverter;
import com.cskaoyan.wordmemorize.dao.entity.OperateLogDO;
import com.cskaoyan.wordmemorize.dao.mapper.OperateLogMapper;
import com.cskaoyan.wordmemorize.dto.admin.OperateLogDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.OperateLogPageRequest;
import com.cskaoyan.wordmemorize.service.AdminLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminLogServiceImpl extends ServiceImpl<OperateLogMapper, OperateLogDO>implements AdminLogService {
    @Autowired
    OperateLogMapper operateLogMapper;
    @Autowired
    AdminLogConverter adminLogConverter;

    @Override
    public PageDTO<OperateLogDTO> getOperateLogPage(OperateLogPageRequest request) {
        if (request == null) {
            throw new BusinessException(ResultCodeEnum.FAIL);
        }
        Integer pageNum = request.getPageNum();
        Integer count = request.getCount();
        String employeeId = request.getEmployeeId();
        String dataType = request.getDataType();

        Page<OperateLogDO> operateLogDOPage = new Page<>(pageNum, count);
        LambdaQueryWrapper<OperateLogDO> wrapper = new LambdaQueryWrapper<OperateLogDO>()
                .like(employeeId != null, OperateLogDO::getEmployeeId, employeeId)
                .like(dataType != null, OperateLogDO::getDataType, dataType);

        Page<OperateLogDO> page = this.page(operateLogDOPage, wrapper);
        List<OperateLogDO> records = page.getRecords();
        long total = page.getTotal();
        return adminLogConverter.operationLogPage2PageDTO(records, total);
    }
}
